<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Download Selected Rows</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        /* Add your custom styles here if needed */
    </style>
</head>
<body>

<div class="container mt-5">
    <h2>Selected Rows to Download</h2>

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>IGSDM</th>
            <th>XXFBRQ</th>
            <th>JZRQ</th>
            <th>XXLY</th>
            <th>GGLJ</th>
            <!-- ... Add more columns if needed ... -->
        </tr>
        </thead>
        <tbody id="downloadTableBody">
            <!-- This is where the selected rows will be added dynamically -->
        </tbody>
    </table>

    <div class="form-group">
        <button type="button" class="btn btn-success mt-2" id="downloadSelectedButton">Download Selected</button>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<script>
    $(document).ready(function () {
        var selectedData = decodeURIComponent(window.location.search.substr(1).split("=")[1]);
        var selectedRows = JSON.parse(selectedData);

        // 用选定的行填充下载表格
        for (var i = 0; i < selectedRows.length; i++) {
            var row = selectedRows[i];
            var newRow = "<tr>" +
                "<td>" + row[0] + "</td>" +
                "<td>" + row[1] + "</td>" +
                "<td>" + row[2] + "</td>" +
                "<td>" + row[3] + "</td>" +
                "<td>" + row[4] + "</td>" +
                "</tr>";
            $("#downloadTableBody").append(newRow);
        }

          $("#downloadSelectedButton").click(function () {
            // 准备用于下载的数据
            var downloadData = selectedRows.map(function(row) {
                return { xxly: row[3], gglj: row[4] ,igsdm:row[0]};
            });

            // 发送请求给后端
            $.ajax({
                type: "POST",
                url: "/download-all",  // 修改为实际的后端下载处理路由
                contentType: "application/json",
                data: JSON.stringify({ selectedRows: downloadData }),
                success: function(response) {
                    console.log(response);  // 处理后端返回的信息
                },
                error: function(error) {
                    console.error(error);
                }
            });
        });
    });
</script>

</body>
</html>
